//
//  YQChatFrame.swift
//  yq微信
//
//  Created by 杨庆 on 2018/1/12.
//  Copyright © 2018年 yq. All rights reserved.
//

import UIKit

/// 聊天界面cell -- 布局模型
class YQChatFrame: NSObject {

    var userIconFrame : CGRect?
    var contentFrame : CGRect?
    var timeFrame : CGRect?
    var cellH : CGFloat? = 0
    
    var chat:YQChat? {
        didSet {
        
            let margin:CGFloat = 10
            let timeH:CGFloat = (chat?.showTime)! ? 20:0 // "ShowTime为true才显示"
            
            let size:CGSize = CGSize(width:CGFloat(MAXFLOAT), height: timeH)
            let timeRect:CGRect = ((chat?.timeStr)! as NSString).boundingRect(with: size, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName:KTimeFont], context: nil)
            let timeW = timeRect.width
            let timeX = (screenW - timeW) * 0.5
            let timeY = margin
            
            self.timeFrame = CGRect(x: timeX, y: timeY, width: timeW, height: timeH)
            
            var iconX : CGFloat
            let iconY = margin + (self.timeFrame?.maxY)!
            let iconW:CGFloat = 44
            let iconH = iconW
            
            var contentX:CGFloat?
            let contentY = iconY
            var contentW:CGFloat?
            var contentH:CGFloat?
           
            /*内容消息:宽度确定,高度不确定*/
            // 内容消息的最大宽度
            let maxContentW = screenW - 2*(iconW + 2*margin) // 286.0
            // 消息内容的宽度/高度
            let contentStrSize : CGRect = ((chat?.contentText!)! as NSString).boundingRect(with: CGSize(width: maxContentW, height: CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName:KContentTextFont], context: nil )
   
            contentW = contentStrSize.width + KContentEdgeLeft + KContentEdgeRight
            contentH = contentStrSize.height + KContentEdgeTop + KContentEdgeBottom
            if (chat?.me)! == true {
                iconX = screenW - margin - iconW
                contentX = iconX - margin - contentW!
            
            }else {
                // 是好友
                iconX = margin
                contentX = iconX + iconW + margin
            }
            
            self.userIconFrame = CGRect(x: iconX, y: iconY, width: iconW, height: iconH)
            self.contentFrame = CGRect(x: contentX!, y: contentY, width: contentW!, height: contentH!)
            
            
            self.cellH = Float((self.userIconFrame?.height)!) > Float((self.contentFrame?.height)!) ? (self.userIconFrame?.maxY)!+margin : (self.contentFrame?.maxY)!+margin
        
        }

    
    }
    
    
    
}
